﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.Practices.Unity;

namespace FluentUnity.Conventions
{
    public class DependencyPropertyScanConvention
    {
        public DependencyPropertyScanConvention()
        {
            Conditions = new List<Func<PropertyInfo, bool>>();
        }

        public void Where(Func<PropertyInfo, bool> condition)
        {
            Conditions.Add(condition);
        }

        private List<Func<PropertyInfo, bool>> Conditions { get; set; }

        internal IEnumerable<InjectionProperty> Scan(Type implementation)
        {
            Ensure.IsNotNull(() => implementation);

            return from p in implementation.GetProperties(BindingFlags.Instance | BindingFlags.Public).ApplyConditions(Conditions)
                   let attribute = p.GetAttribute<DependencyAttribute>()
                   where attribute != null && p.CanWrite
                   select new InjectionProperty(p.Name);
        }
    }
}